package com.sansan.qiangji.realize.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.base.deviceAlarm.DeviceAlarmListResultVO;
import com.sansan.qiangji.VO.base.deviceAlarm.DeviceAlarmVO;
import com.sansan.qiangji.entity.base.DeviceAlarm;
import com.sansan.qiangji.entity.base.Stock;
import com.sansan.qiangji.entity.base.example.DeviceAlarmExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.DeviceAlarmAddForm;
import com.sansan.qiangji.form.base.DeviceAlarmEditForm;
import com.sansan.qiangji.form.base.DeviceAlarmListForm;
import com.sansan.qiangji.service.inter.base.DeviceAlarmService;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DeviceAlarmReal {
    
    @Autowired
    private DeviceAlarmService deviceAlarmService;


    @Transactional
    public void deleteDeviceAlarm(Integer deviceAlarmId) {
        DeviceAlarm deviceAlarm = deviceAlarmService.selectByPrimaryKey(deviceAlarmId);
        if (deviceAlarm == null) {
            log.error("【报警删除】报警不存在，deviceAlarmId={}", deviceAlarmId);
            throw new ServiceException(ResultEnum.DEVICE_ALARM_IS_NOT_EXIST);
        }
        int row = deviceAlarmService.deleteByPrimaryKey(deviceAlarmId);
        if (row == 0) {
            log.error("【报警删除】报警删除失败，deviceAlarmId={}", deviceAlarmId);
            throw new ServiceException(ResultEnum.DEVICE_ALARM_DELETE_ERROR);
        }
    }

    @Transactional
    public void addDeviceAlarm(DeviceAlarmAddForm deviceAlarmAddForm) {
        DeviceAlarmExample example = new DeviceAlarmExample();
        example.or().andDeviceAlarmNameEqualTo(deviceAlarmAddForm.getDeviceAlarmName());
        List<DeviceAlarm> deviceAlarmList = deviceAlarmService.selectByExample(example);
        if (deviceAlarmList.size() > 0) {
            log.error("【报警新增】该报警名称或编号已存在，deviceAlarmName={}", deviceAlarmAddForm.getDeviceAlarmName());
            throw new ServiceException(ResultEnum.DEVICE_ALARM_IS_EXIST);
        }
        DeviceAlarm deviceAlarm = new DeviceAlarm();
        BeanUtils.copyProperties(deviceAlarmAddForm, deviceAlarm);
        int row = deviceAlarmService.insert(deviceAlarm);
        
        if (row == 0) {
            log.error("【报警新增】报警新增失败");
            throw new ServiceException(ResultEnum.DEVICE_ALARM_INSERT_ERROR);
        }
    }


    @Transactional
    public void editDeviceAlarm(DeviceAlarmEditForm deviceAlarmEditForm) {
         
        DeviceAlarm deviceAlarm = new DeviceAlarm();
        BeanUtils.copyProperties(deviceAlarmEditForm, deviceAlarm);
        int row = deviceAlarmService.updateByPrimaryKey(deviceAlarm);
        if (row == 0) {
            log.error("【报警新增】报警新增失败");
            throw new ServiceException(ResultEnum.DEVICE_ALARM_INSERT_ERROR);
        }
    }

    public DeviceAlarmListResultVO deviceAlarmList(DeviceAlarmListForm deviceAlarmListForm){

        DeviceAlarmExample example = new DeviceAlarmExample();
        int pageNum = deviceAlarmListForm.getPageNum();
        int pageSize = deviceAlarmListForm.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        DeviceAlarmExample.Criteria criteria = example.createCriteria();
        if(deviceAlarmListForm.getDeviceAlarmName()!=null&&!deviceAlarmListForm.getDeviceAlarmName().equals ("")){
            criteria.andDeviceAlarmNameLike("%"+deviceAlarmListForm.getDeviceAlarmName()+"%");
        }
        if(deviceAlarmListForm.getDeviceAlarmType()!=null&&!deviceAlarmListForm.getDeviceAlarmType().equals ("")){
            criteria.andDeviceAlarmTypeLike("%"+deviceAlarmListForm.getDeviceAlarmType()+"%");
        }
        List<DeviceAlarm> deviceAlarmList = deviceAlarmService.selectByExample(example);
        PageResult<DeviceAlarm> deviceAlarmPageResult = PageUtil.getPageResult(new PageInfo<>(deviceAlarmList));
        DeviceAlarmListResultVO deviceAlarmListResultVO = new DeviceAlarmListResultVO();
        BeanUtils.copyProperties(deviceAlarmPageResult,deviceAlarmListResultVO);
        List<DeviceAlarmVO>  deviceAlarmVOS = new ArrayList<>();
        for(DeviceAlarm deviceAlarm : deviceAlarmPageResult.getContent()){
            DeviceAlarmVO deviceAlarmVO = new DeviceAlarmVO();
            BeanUtils.copyProperties(deviceAlarm,deviceAlarmVO);
            deviceAlarmVOS.add(deviceAlarmVO);
        }
        deviceAlarmListResultVO.setDeviceAlarmVOS(deviceAlarmVOS);
        return deviceAlarmListResultVO;
    }

}
